library(tidyverse)
library(cjoint)
library(cregg)
library(xtable)
library(ggplot2)
library(graphics)

###SET WORKING DIRECTORY and LOAD FILES ----

set.seed(12345678)
df <- read.csv("b_data/3_long_data_for_analysis.csv")


####Reshaping data as factors ----

df$race <- as.factor(df$race)

df <- df %>% 
  mutate(Faces = factor(Faces, levels = c("White man", "White woman", "Light man", "Light woman", 
                                          "Medium man",   "Medium woman", "Brown man", 
                                          "Brown woman", "Black man", "Black woman"), 
                        ordered = is.ordered(Faces)))


df <- df %>%
  mutate(Background = factor(Background, levels = c("Legal immigrant",
                                                    "Undoc. immigrant",
                                                    "Naturalized citizen",
                                                    "US-born legal parents",
                                                    "US-born undoc. parents",
                                                    "US-born legal grandparents",
                                                    "US-born undoc. grandparents")))

df <- df %>%
  mutate(Identifies = factor(Identifies, levels = c("American",
                                                    "Latino",
                                                    "Hispanic",
                                                    "Mexican-American",
                                                    "Mexican",
                                                    "White",
                                                    "Black")))

df <- df %>%
  mutate(Language = factor(Language, levels = c("Only English",
                                                "Bilingual",
                                                "Limited English",
                                                "No English"),
                           ordered = is.ordered(Language)))

df <- df %>%
  mutate(Education = factor(Education, levels = c("High school dropout",
                                                  "High school graduate",
                                                  "Bachelor's degree",
                                                  "Advanced/graduate degree"),
                            ordered = is.ordered(Education)))

df <- df %>%
  mutate(Profession = factor(Profession, levels = c("Waiter", "Janitor",
                                                    "Sales manager", "Teacher",
                                                    "IT professional", "Doctor",
                                                    "Small business owner")))

df <- df %>%
  mutate(Religion = factor(Religion, levels = c("Presbyterian", "Evangelical",
                                                "Catholic", "Not religious")))

df <- df %>%
  mutate(Political = factor(Political, levels = c("Democrat", "Republican",
                                                  "Independent", "Not political")))

df <- df %>%
  mutate(Spouse = factor(Spouse, levels = c("Not married",
                                            "S. is American",
                                            "S. is Black",
                                            "S. is White",
                                            "S. is same as profile")))


###CONJOINT DESIGN ----

attribute_list <- list()

attribute_list[["Faces"]] <-c("White man", "White woman", "Light man", "Light woman",
                              "Medium man", "Medium woman", "Brown man", "Brown woman",
                              "Black man", "Black woman")
attribute_list[["Background"]] <- c("Legal immigrant", "Undoc. immigrant",
                                    "Naturalized citizen", 
                                    "US-born legal parents",
                                    "US-born undoc. parents",
                                    "US-born legal grandparents",
                                    "US-born undoc. grandparents")
attribute_list[["Identifies"]] <- c("American", "Latino",
                                    "Hispanic", "Mexican-American",
                                    "Mexican", "White", "Black")
attribute_list[["Language"]] <- c("Only English",
                                  "Bilingual",
                                  "Limited English",
                                  "No English")
attribute_list[["Education"]] <- c("High school dropout",
                                   "High school graduate",
                                   "Bachelor's degree",
                                   "Advanced/graduate degree")
attribute_list[["Profession"]] <- c("Waiter", "Janitor",
                                    "Sales manager", "Teacher",
                                    "IT professional", "Doctor",
                                    "Small business owner")
attribute_list[["Religion"]] <- c("Presbyterian", "Evangelical",
                                  "Catholic", "Not religious")
attribute_list[["Political"]] <- c("Democrat", "Republican",
                                   "Independent", "Not political")
attribute_list[["Spouse"]] <- c("Not married", "S. is American",
                                "S. is Black", "S. is White",
                                "S. is same as profile")



constraint_list <- list()

constraint_list[[1]] <- list()
constraint_list[[1]][["Education"]] <- c("High school dropout",
                                         "High school graduate")
constraint_list[[1]][["Profession"]] <- c("Doctor",
                                          "IT professional", "Teacher")

constraint_list[[2]] <- list()
constraint_list[[2]][["Education"]] <- c("Bachelor's degree")
constraint_list[[2]][["Profession"]] <- c("Doctor")


constraint_list[[3]] <- list()
constraint_list[[3]][["Background"]] <- c("US-born legal parents",
                                          "US-born undoc. parents",
                                          "US-born legal grandparents",
                                          "US-born undoc. grandparents")
constraint_list[[3]][["Language"]] <- c("No English")

constraint_list[[4]] <- list()
constraint_list[[4]][["Background"]] <- c("Legal immigrant",
                                          "Naturalized citizen",
                                          "Undoc. immigrant")
constraint_list[[4]][["Language"]] <- c("Only English")

constraint_list[[5]] <- list()
constraint_list[[5]][["Profession"]] <- c("IT professional",
                                          "Doctor",
                                          "Sales manager",
                                          "Teacher")
constraint_list[[5]][["Language"]] <- c("No English")

constraint_list[[6]] <- list()
constraint_list[[6]][["Faces"]] <- c("White man", "White woman", 
                                     "Light man", "Light woman",
                                     "Medium man", "Medium woman")
constraint_list[[6]][["Identifies"]] <- c("Black")

constraint_list[[7]] <- list()
constraint_list[[7]][["Faces"]] <- c("Brown man", "Brown woman",
                                     "Black man", "Black woman")
constraint_list[[7]][["Identifies"]] <- c("White")

marginal_weights <- list()
marginal_weights[["Faces"]] <- rep(1/length(attribute_list[["Faces"]]),
                                   length(attribute_list[["Faces"]]))
marginal_weights[["Background"]] <- rep(1/length(attribute_list[["Background"]]),
                                        length(attribute_list[["Background"]]))
marginal_weights[["Identifies"]] <- rep(1/length(attribute_list[["Identifies"]]),
                                        length(attribute_list[["Identifies"]]))
marginal_weights[["Language"]] <- rep(1/length(attribute_list[["Language"]]),
                                      length(attribute_list[["Language"]]))
marginal_weights[["Education"]] <- rep(1/length(attribute_list[["Education"]]),
                                       length(attribute_list[["Education"]]))
marginal_weights[["Profession"]] <- rep(1/length(attribute_list[["Profession"]]),
                                        length(attribute_list[["Profession"]]))
marginal_weights[["Religion"]] <- rep(1/length(attribute_list[["Religion"]]),
                                      length(attribute_list[["Religion"]]))
marginal_weights[["Political"]] <- c(2/9, 2/9, 2/9, 3/9)
marginal_weights[["Spouse"]] <- c(5/15, 3/15, 2/15, 2/15, 3/15)

conjointdesign <- cjoint::makeDesign(type='constraints', attribute.levels=attribute_list,
                                     constraints=constraint_list, level.probs=marginal_weights)
baselines <- list()
baselines$Education <- "High school graduate"
baselines$Background <- "US-born legal grandparents"
baselines$Profession <- "Teacher"
baselines$Political <- "Not political"
baselines$Religion <- "Not religious"


####Feature order -----
c_order <- c("Faces", "Identifies", "Religion", "Background", "Language",
             "Spouse", "Education", "Profession", "Political")

#### AMCE of Perceptions of Americanness (Amerian rating) ----
####NOTE: This model is computationally intensive and may not run on all computers


ratingamce_r <- cjoint::amce(rating_r ~ Faces + Background + Identifies +
                               Language + Education + Profession + Religion 
                             + Political + Spouse, data=df, cluster=TRUE, 
                             respondent.id="caseid", design=conjointdesign,
                             weights="weight", baselines=baselines,
                             na.ignore=TRUE)



####Figure 1 ----
lim <- c(-.25, .05)

pdf("c_figures/figure_1.pdf", width = 8.5, height = 11)

figure_1 <- plot(ratingamce_r, main = "Average Marginal Component Effect of Perceived Americanness Among All Respondents",
  xlab = "Change: American Rating (0 'not at all American' - 1 'very much American')", group.order= c_order,
  colors="black",
  xlim=lim,
   point.size=0.4, 
   dodge.size=1,
  plot.theme = (ggplot2::theme_bw() + theme(legend.position="none", plot.title= element_text(size=10)))) 

dev.off()


####Table A5----
summary(ratingamce_r)
sum_amce_r <- summary(ratingamce_r)
amce_r <-sum_amce_r$amce
amcer_table <- amce_r[c(2:6)]
print.xtable(xtable(amcer_table), "d_tables/table_A5.tex", type = "latex", include.rownames=FALSE)

###Marginal mean differences in perceptions of Americanness by race model----
american_diff <- cregg::mm_diffs(na.omit(df),
                                 rating_r ~ Faces + Background
                                 + Education + Profession
                                 + Language + Political
                                 + Religion + Spouse
                                 + Identifies,
                                 by = ~ race,
                                 weights = ~weight, 
                                 feature_order = c_order)

####Figure 2----
american_diff_p <- plot(american_diff)

figure_2 <- american_diff_p + ggplot2::theme_bw() + 
  theme(legend.position="none", plot.title= element_text(size=10)) +
  scale_color_manual(values = rep("black", 9)) +
  geom_point(size = 2) + 
  ggplot2::labs(x = "Estimated Difference Between White and Black Respondents",
                title = "Marginal Mean Differences in Perceived Americanness Between White and Black Respondents") 

ggsave("c_figures/figure_2.pdf", width=8.5, height=11, plot = figure_2)

####Table A6 ----
american_diff_table <- american_diff[c(5:9)]
print.xtable(xtable(american_diff_table), "d_tables/table_A6.tex", type = "latex", include.rownames=FALSE)

#### Clear environment ----

rm(list = ls())
